import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/sy',
    children:[
      {
        path: '/sy',
        name: 'sy',
        component: () => import('../views/sy.vue')
      },
      {
        path: '/hy',
        name: 'hy',
        component: () => import('../views/hy.vue')
      },
      {
        path: '/xc',
        name: 'xc',
        component: () => import('../views/xc.vue')
      },
      {
        path: '/dingdan',
        name: 'dingdan',
        component: () => import('../views/dingdan.vue')
      },
      {
        path: '/shop',
        name: 'shop',
        component: () => import('../views/shop.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.name=='hy'||to.name=='dingdan'||to.name=='shop'||to.name=='xc'||to.name=='sy'){
    if(localStorage.getItem('token')){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})

export default router
